package com.edvinli.controller;

import com.edvinli.annotation.MyLog;
import com.edvinli.entity.Dept;
import com.edvinli.entity.Result;
import com.edvinli.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 查询部门列表
     * @return
     */
    @GetMapping
    public Result getAll(){
        List<Dept> depts = deptService.list();

        //3.响应数据（json格式）
        return Result.success(depts);
    }
    /**
     * 根据id删除部门     根据名称新增部门
     * @param id
     */
    @MyLog
    @DeleteMapping
    public Result delete(Integer id){
        log.info("id = {}", id);
        deptService.delete(id);

        return Result.success();
    }

    /**
     * 根据名称新增部门
     * @param dept
     * @return
     */
    @MyLog
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("dept = {}", dept);
        deptService.add(dept);

        return Result.success();
    }

    /**
     * 根据id查询来回显信息
     * @param id
     * @return
     * @PathVariable用于接收路径参数值
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("id = {}", id);
        Dept dept = deptService.getById(id);

        return Result.success(dept);
    }

    /**
     * 更新部门数据
     * @param dept
     * @return
     */
    @MyLog
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("dept = {}", dept);

        deptService.update(dept);

        return Result.success();
    }
}
